<?php
namespace Home\Controller;
use Think\Controller;
header("Content-Type: text/html;charset=utf-8");
class ContentController extends Controller {

      public function _initialize()
    	{
        if(!session('?users')){
            $this->error('没有权限','../Login/login');
            exit;
        }
    }

    public function persion($id){
    	$User = M('Users'); // 实例化User对象
      $list = $User->where("id=$id")->select();

   	  $User1 = M('members'); // 实例化User对象
    	$list1 = $User1->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("users.id=$id")->select();


       

      	$this->groups=$list1;
        $this->users=$list;
        $this->display(); // 输出模板
    }
    public function passwordModify1(){
        $user = M('Users'); 
        $pass1=md5($_POST['pass1']);
        $pass2=md5($_POST['pass2']);
        $pass3=md5($_POST['pass3']);
        $user_id=$_POST['id'];
        if(!$pass2=$pass3){
            $this->error('密码不一致');
        }else{
            $users=$user->where("id=$user_id")->select();
            if($users){
                $pass=$users[0]['pass'];
                if($pass==$pass1){
                    $user->pass=$pass2;
                    $user->id=$user_id;
                    if($user->save()){
                        $this->success('修改成功','../Index/index');
                    }else{
                        echo "$pass2";
                        var_dump($users);
                         #$this->error('修改失败');
                    }

                }else{
                  $this->error('旧密码错误');
                }
            }else{
                 $this->error('找失败');
            }
    }
}
 public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/uploads/photo/'; // 设置附件上传根目录
       
        $upload->autoSub=false;
        $info= $upload->upload();  // 上传文件 
         if(!$info) {
               $this->error($upload->getError()); 
        }
        else{ 
              return $info;    
          }                                     

    }
    public function fupload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('doc','text','xlsx','xls','txt');// 设置附件上传类型
        $upload->rootPath  =     './Public/uploads/file/'; // 设置附件上传根目录
       
        $upload->autoSub=false;
        $info= $upload->upload();  // 上传文件 
         if(!$info) {
               $this->error($upload->getError()); 
        }
        else{ 
              return $info;    
          }                                     

    }
public function userUpdate()
{
     $user=M("users");
        $user->create();
        
        if($_FILE['photo']['error']==0){
            $photoinfo=$this->upload();
            $user->photo=$photoinfo['photo']['savename'];
        }else{
             
        }
      
        if ($user->save()) {
                 $this->success('修改成功','../Index/index',4);
        } else{
                
                $this->error("修改失败");
        }
}


public function userModify($id){
      $user = M('Users');
      $users=$user->where("id=$id")->find();
      $this->users=$users;
      $this->display();
}


public function findGroups($num){
  $groupsModel=M('groups');
  $this->groups=$groupsModel->where("leaders=$num")->select();

  $this->display('groupsMemberAdd');

}
public function findGroups1(){
  $num=$_SESSION['num'];
  $groupsModel=M('groups');
  $this->groups=$groupsModel->where("leaders=$num")->select();

  $this->display('groupsActivityAdd');

}

public function groupsMemberAdd(){
    $num=$_POST['num'];
    $realname=$_POST['realname'];
    $userModel=M('users');
    $users=$userModel->where("num=$num")->select();
    if($users){
      
   
      $memberModel=M('members');
      $memberModel->create();
      $memberModel->users_id=$users[0]['id'];
      $memberModel->addtime=date("Y-m-d");
      $memberModel->state=2;
      if($memberModel->add()){
        $this->success('添加成功','../Index/index');
      }else{
        $this->error('no');
      }

    }else{
      $this->error('用户名或学号错误');
    }
  
}

public function isSuccess($num){
   $groupsModel=M('Groups');
   $groups=$groupsModel->where("leaders=$num")->select();

   
   if($groups){
      $groups_id=$groups[0]['id'];
      $memberModel=M('members');
      $this->members=$memberModel->where("groups_id=$groups_id And state1=1")->select();

     
      $this->display();
   }else{
    $this->error("no");
   }
}

public function isOutg($num){
   $groupsModel=M('Groups');
   $groups=$groupsModel->where("leaders=$num")->select();
   if($groups){
      $groups_id=$groups[0]['id'];
      $memberModel=M('members');
      $this->members=$memberModel->where("groups_id=$groups_id And zt=1")->select();
      $this->display();
   }else{
    $this->error("no");
   }
}
public function isOuta($num){
   $groupsModel=M('stuprac');
   $groups=$groupsModel->where("leaders=$num")->select();

   
   if($groups){
      $groups_id=$groups[0]['id'];
      $memberModel=M('stuprac');
      $this->members=$memberModel->where("groups_id=$groups_id And state1=1")->select();

     
      $this->display();
   }else{
    $this->error("no");
   }
}


public function groupsModify($num)
{
   $usersmodel=M('groups');
        $aa=$usersmodel->where("leaders=$num")->select();
        $this->aa=$aa;
        $this->display();
}

public function groupsUpdate(){
        $user=M("groups");
        $user->create();
        
        if($_FILE['logo']['error']==0){
            $photoinfo=$this->upload();
            $user->logo=$photoinfo['logo']['savename'];
        }else{
             $user->logo=$_POST['photo'];
        }
       
        if ($user->save()) {
                 $this->success('修改成功','Index/index');
        } else{
                
                $this->error("修改失败");
        }
    }

    public function myActivity()
    {
       $users_id= $_SESSION['users_id'];
        
      $User1 = M('stuprac'); // 实例化User对象
      $list1 = $User1->join("activity ON activity_id =activity.aid")->join("users ON users_id =users.id")->where("users.id=$users_id")->select();
      $this->groups=$list1;
      $this->display('myActivityList');

      
    }

    public function myGroups()
    {
      $users_id= $_SESSION['users_id'];
        
      $User1 = M('members'); // 实例化User对象
      $list1 = $User1->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("users.id=$users_id")->select();
      $this->groups=$list1;
      $this->display('mygroupsList');
    }

    public function activity1Add(){
      $groups_id=$_POST['group_id'];

      $groupModel=M('groups');
      $gg=$groupModel->where("id=$groups_id")->select();

      $leader=$gg[0]['leaders'];

      $activityModel=M('activity');
      $activityModel->create();
      $activityModel->leader=$leader;

     if($_FILE['pfile']['error']==0){
            $photoinfo=$this->fupload();
            $activityModel->pfile=$photoinfo['pfile']['name'];
        }
    
    $activityModel->hit=0;
    $activityModel->pdate=date('Y-m-d');
    

    $activitya=$activityModel->add();

    if($activitya){
        $this->success('添加成功','../Index/index');
    }else{

        $this-error('no');
    }

   }
   public function findFile(){
     $activityModel=M('activity');
     $this->file=$activityModel->select();
     $this->display('downFile');
   }
    public function groupsList(){
        $User = M('groups'); // 实例化User对象
        $count = $User->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $where['groupname']=array('like','%'.$_POST['name'].'%');
        $list = $User->where($where)->order('id desc')->select();

  
        
        $this->groups=$list;
        $this->page=$show;
        
       

        $this->display(); // 输出模板
    }
    
    public function activityList(){
        $User = M('activity'); // 实例化User对象
        $count = $User->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(5)
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $where['name']=array('like','%'.$_POST['name'].'%');
        $list = $User->join('groups ON group_id = groups.id')->order('activity.aid desc')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();

       
        
        $this->groups=$list;
        $this->page=$show;
        
        $this->display(); // 输出模板
    }
   

    public function newsList(){
        $User = M('news'); // 实例化User对象
       
        $where['title']=array('like','%'.$_POST['name'].'%');
        $list = $User->join('activity ON activity_id =activity.aid')->where($where)->order('id desc')->select();

        $this->groups=$list;
        $this->page=$show;
        $this->display(); // 输出模板
    }

    public function groupsMemberList($id){
      $User = M('members'); // 实例化User对象
    
    $list = $User->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("groups_id=$id")->select();

        $this->groups=$list;
        $this->page=$show;
        $this->groups_id=$id;
        $this->display(); // 输出模板
   }

    public function groupsActivityList($id){
      $User = M('activity'); // 实例化User对象
    
    $list = $User->join("groups ON group_id =groups.id")->where("group_id=$id")->select();

        $this->groups=$list;
       
        $this->groups_id=$id;
        $this->display(); // 输出模板
   }

   public function groupsInfo($id){
     $User = M('groups'); // 实例化User对象
     $list = $User->where("id=$id")->select();

    
    

    $User1 = M('members'); // 实例化User对象

    $list1 = $User1->join("groups ON groups_id =groups.id")->join("users ON users_id =users.id")->where("groups_id=$id")->select();
    $User2 = M('activity'); // 实例化User对象
    $list2 = $User2->where("group_id=$id")->select();

        $User3 = M('stuprac'); // 实例化User对象
    
        $list3 = $User3->join("activity ON activity_id =activity.aid")->join("users ON users_id =users.id")->select();

        $this->groups=$list;
        $this->users=$list1;
        $this->activity=$list2;
        $this->stuprac=  $list3;
        $this->display(); // 输出模板
}

       public function groupsExit($id){
           $users_id= $_SESSION['users_id'];
           $aaModel=M('members');
            $aaModel->zt=1;
           $bb=$aaModel->where("groups_id=$id AND users_id=$users_id")->save();


           var_dump($bb);
           if($bb){
            $this->redirect('myGroups');
           }else{
            $this->error('no');
           }
       }

        public function activityExit($id){
           $users_id= $_SESSION['users_id'];
           $aaModel=M('stuprac');
           $aaModel->zt=1;
           $bb=$aaModel->where("activity_id=$id AND users_id=$users_id")->save();
           if($bb){
            $this->redirect('myActivity');
           }else{
            $this->error('no');
           }
       }


       public function shenqing($id){ 
        $users_id= $_SESSION['users_id'];
        $memberModel=M('members');
        $memberModel->groups_id=$id;
        $memberModel->users_id=$users_id;
        $memberModel->addtime=date("Y-m-d");
        $memberModel->state1=2;
        if($memberModel->add()){
          $this->success("申请成功");
        }else{
          $this->error('no');
        }

       }
}